<?php

namespace App\Http\Controllers\Rules;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class RulesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        
        $data  = DB::table("spb_rules")->orderBy("id","desc")->get();
        //dd($data);
        return view("rules.index",["list"=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("rules/add");
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->input();
        array_shift($data);
       // dd($data);
       $data['created_at'] = date("Y-m-d H:i:s");
        $re = DB::table("spb_rules")->insert($data);
        if($re){
                return redirect("rules/index");

        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $id = $request->input("id");
        $data = DB::table("spb_rules")->where("id",$id)->get();
        //dd($data);
        return view("rules/edit",["data"=>$data[0]]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $data = $request->input();
        array_shift($data);
        $id = $data['id'];
        array_shift($data);
        $data['updated_at'] = date("Y-m-d H:i:s");
        $re = DB::table("spb_rules")->where("id",$id)->update($data);
        if($re){
            return redirect("rules/index");
        }else{
            return redirect("rules/index");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $id = $request->input("id");
        $re = DB::table("spb_rules")->where("id",$id)->delete();
        if($re){

            return redirect("rules/index");
        }
    }
}
